<template>
	<view class="" v-if="pageShow">
		<view class="margin_top5 flexcenter dingwei" @click="uploadImg(1,avatar,avatar_text)">
			<image  class="touxiang" :src="avatar_text!=''?avatar_text:'@/static/image/system/weidenglu.png'" mode=""></image>
			<image class="xianj" src="@/static/image/sysem/xj.png" mode=""></image>
		</view>
		<view class="margin_top4 tybjs mainpadding flexbetween">
			<view class="sanshier xiaobai">昵称</view>
			<input type="text" placeholder="请输入" style="text-align: right;" class="xiaobai main_size bold" v-model="nickname">
		</view>
		<view class="margin_top tybjs mainpadding flexbetween" @click="selectsex()">
			<view class="sanshier xiaobai">性别</view>
			<view class="flexright">
				<view class="sanshier xiaohui nofonweight margin_right2">{{gender==1?"男":"女"}}</view>
				<u-icon name="arrow-right" color="#938F90" size="14"></u-icon>
			</view>
		</view>
		<view class="margin_top4 tybjs mainpadding ">
			<view class="sanshier xiaobai">个人简介</view>
			<textarea class="margin_top xiaobai main_size bold" placeholder="请输入个人简介" v-model="bio"></textarea>
		</view>
		<view class="gudingdb  mainpadding">
			<view class="bigbtn" @click="submit">提交</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar:"",
				avatar_text:"",
				nickname:"",
				bio:"",
				gender:0,
				pageShow:false,
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			submit(){
				httpRequest.request('/api/user/profile', 'GET', {
					nickname:this.nickname,
					avatar:this.avatar,
					gender:this.gender,
					bio:this.bio
				}, false, false, true).then(res => {
					httpRequest.toast(res.msg)
					if(res.code==1){
						setTimeout(()=>{
							uni.navigateBack(1)
						},1000)
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}, false, false, true).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
					this.gender = res.data.gender
					this.bio = res.data.bio
					this.pageShow = true
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			uploadImg(value, upImgs,upImgs_text) {
				let _this = this;
				uni.showActionSheet({
					itemList: ['相机', '相册'],
					success: function(res) {
						// 选择相机走0
						if (res.tapIndex == 0) {
							httpRequest.chooseImage(1, ['camera']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
						// 选择相册走1
						if (res.tapIndex == 1) {
							httpRequest.chooseImage(1, ['album']).then(res => {
								for (var i = 0; i < res.length; i++) {
									_this.uploadImageService(res[i], upImgs,upImgs_text)
								}
							}).catch(err => {});
						}
					},
					fail: function(res) {}
				});
			},
			uploadImageService(url, upImg,upImgs_text) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					let image_item = res.fullurl;
					this.avatar = res.url;
					this.avatar_text = image_item
				}).catch(err => {});
			},
			selectsex(){
				let that = this
				uni.showActionSheet({
					itemList: ['男', '女'],
					success: function(res) {
						that.gender = res.tapIndex?2:1 
					},
				});
			},
		}
	}
</script>

<style lang="scss" scoped>
	.xianj {
		width: 46.13rpx;
		height: 46.13rpx;
		position: absolute;
		bottom: 0;
		right: 40%;
	}

	.touxiang {
		width: 182rpx;
		height: 182rpx;
		border-radius: 50%;
	}
</style>